package cn.newgrand.netcall.ui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.jivesoftware.resource.Res;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.TitlePanel;
import org.jivesoftware.spark.util.ResourceUtils;
import org.jivesoftware.spark.util.SwingWorker;

import cn.newgrand.smackx.specialist.ExpertManager;
import cn.newgrand.smackx.specialist.packet.ExpertVcardIQ;

public class ExpertInfoConfig extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private JLabel spnameLabel = new JLabel();
	private JTextField spnameField = new JTextField(12);
	private JLabel emailLabel = new JLabel();
	private JTextField emailField = new JTextField();
	private JLabel specialtyLabel = new JLabel();
	private JTextField specialtyField = new JTextField();
	private JLabel companyLabel = new JLabel();
	private JTextField companyField = new JTextField();
	private JLabel telephoneLabel = new JLabel();
	private JTextField telephoneField = new JTextField();
	private JLabel departmentLabel = new JLabel();
	private JTextField departmentField = new JTextField();
	private JLabel facsimileLabel = new JLabel();
	private JTextField facsimileField = new JTextField();
	private JLabel welcomeinfoLabel = new JLabel();
	private JTextArea welcomeinfoArea;
	private JLabel maxuserLabel = new JLabel();
	private JComboBox maxuserBox = new JComboBox();
	private String spid;
	
	public ExpertInfoConfig(String expertname){
		this.setLayout(new GridBagLayout());
		spid = expertname;
		this.add(spnameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		this.add(spnameField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		this.add(emailLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		this.add(emailField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		this.add(specialtyLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		this.add(specialtyField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		this.add(companyLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		this.add(companyField, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		this.add(telephoneLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		this.add(telephoneField, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		this.add(departmentLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		this.add(departmentField, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		this.add(facsimileLabel, new GridBagConstraints(0, 6, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		this.add(facsimileField, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		this.add(maxuserLabel, new GridBagConstraints(0, 7, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		String[] product = {"5","10","15","20","25"};
		for(int i = 0; i < product.length; i++){
			maxuserBox.addItem(product[i]);
		}
		this.add(maxuserBox, new GridBagConstraints(1, 7, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		this.add(welcomeinfoLabel, new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		welcomeinfoArea = new JTextArea(8,5);
		welcomeinfoArea.setLineWrap(true);
        JScrollPane welcomeinfoScrollPane = new JScrollPane(welcomeinfoArea);
		this.add(welcomeinfoScrollPane, new GridBagConstraints(1, 8, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		
		ResourceUtils.resLabel(spnameLabel, spnameField, "姓名:");
		ResourceUtils.resLabel(emailLabel, emailField, "邮箱:");
		ResourceUtils.resLabel(specialtyLabel, specialtyField, "专长:");
		ResourceUtils.resLabel(companyLabel, companyField, "公司:");
		ResourceUtils.resLabel(telephoneLabel, telephoneField, "电话:");
		ResourceUtils.resLabel(departmentLabel, departmentField, "部门:");
		ResourceUtils.resLabel(facsimileLabel, facsimileField, "传真:");
		ResourceUtils.resLabel(maxuserLabel, maxuserBox, "队列中最大人数:");
		ResourceUtils.resLabel(welcomeinfoLabel, "欢迎辞:");
		
		createDialog();
	}
	
	public void createDialog(){
		SwingWorker worker = new SwingWorker() {
			ExpertVcardIQ expertVcardIQ = null;
			@Override
			public Object construct() {
				expertVcardIQ = ExpertManager.getInstance(SparkManager.getConnection()).getExpertVcardInfo(spid);
				return "OK";
			}
			
			public void finished(){
				spnameField.setText(expertVcardIQ.getSpname());
				emailField.setText(expertVcardIQ.getEmail());
				specialtyField.setText(expertVcardIQ.getSpecialty());
				companyField.setText(expertVcardIQ.getCompany());
				telephoneField.setText(expertVcardIQ.getTelephone());
				departmentField.setText(expertVcardIQ.getDepartment());
				facsimileField.setText(expertVcardIQ.getFacsimile());
				maxuserBox.setSelectedItem(expertVcardIQ.getMaxuser());
				welcomeinfoArea.setText(expertVcardIQ.getWelcomeinfo());
			}
		};
		worker.start();
		final JOptionPane pane;
        final JDialog dlg;

        TitlePanel titlePanel;

        // Create the title panel for this dialog
        titlePanel = new TitlePanel("设置"+spid+"专家资料", "填写专家相应的信息后保存", SparkRes.getImageIcon(SparkRes.BLANK_24x24), true);

        // Construct main panel w/ layout.
        final JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        // The user should only be able to close this dialog.
        Object[] options = {"保存", Res.getString("close")};
        pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);

        mainPanel.add(pane, BorderLayout.CENTER);

        JOptionPane p = new JOptionPane();
        dlg = p.createDialog(SparkManager.getMainWindow(), "设置专家资料");
        dlg.pack();
        dlg.setSize(400, 550);
        dlg.setContentPane(mainPanel);
        dlg.setLocationRelativeTo(SparkManager.getMainWindow());


        PropertyChangeListener changeListener = new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
                Object o = pane.getValue();
                if (o instanceof Integer) {
                    dlg.setVisible(false);
                    return;
                }

                String value = (String)pane.getValue();
                if (Res.getString("close").equals(value)) {
                    dlg.setVisible(false);
                }
                else if ("保存".equals(value)) {
                    boolean isValid = validatePanel();
                    if(isValid){
                    	ExpertVcardIQ expertVcardIQ = new ExpertVcardIQ();
                    	expertVcardIQ.setSpid(spid);
                    	expertVcardIQ.setCompany(companyField.getText());
                    	expertVcardIQ.setDepartment(departmentField.getText());
                    	expertVcardIQ.setEmail(emailField.getText());
                    	expertVcardIQ.setFacsimile(facsimileField.getText());
                    	expertVcardIQ.setMaxuser((String)maxuserBox.getSelectedItem());
                    	expertVcardIQ.setSpecialty(specialtyField.getText());
                    	expertVcardIQ.setSpname(spnameField.getText());
                    	expertVcardIQ.setTelephone(telephoneField.getText());
                    	expertVcardIQ.setWelcomeinfo(welcomeinfoArea.getText());
                    	ExpertManager.getInstance(SparkManager.getConnection()).updateExpertInfo(expertVcardIQ);
                    	dlg.setVisible(false);
                    }else{
                    	pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                    }
                }
            }

        };

        pane.addPropertyChangeListener(changeListener);
        dlg.setResizable(true);
        spnameField.requestFocusInWindow();

        dlg.setVisible(true);
        dlg.toFront();
        dlg.requestFocus();
	}
	
	/**
	 * 判断用户提交的信息是否合法为空.
	 * @return
	 */
	private boolean validatePanel() {
		String company = spnameField.getText().trim();
		String teltphone = telephoneField.getText().trim();
		String specialty = specialtyField.getText().trim();
		if("".equals(company)){
			JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "姓名不能为空", "警告对话框", JOptionPane.WARNING_MESSAGE);
			spnameField.requestFocus();
			return false;
		}
		if("".equals(specialty)){
			JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "专长不能为空", "警告对话框", JOptionPane.WARNING_MESSAGE);
			specialtyField.requestFocus();
			return false;
		}
		if("".equals(teltphone)){
			JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "电话不能为空", "警告对话框", JOptionPane.WARNING_MESSAGE);
			telephoneField.requestFocus();
			return false;
		}
		return true;
	}

	public String getSpid() {
		return spid;
	}

	public void setSpid(String spid) {
		this.spid = spid;
	}
	
	

}
